package top.kaoshanji.r5ch12d;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/5/11 10:19
 */
@Service("userService")
public class UserService extends BaseService {

    private JdbcTemplate jdbcTemplate;
    private ScoreService scoreService;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Autowired
    public void setScoreService(ScoreService scoreService) {
        this.scoreService = scoreService;
    }

    public void logon(String userName) {
        System.out.println("...运行于本服务updateLastLogonTime方法之前...");
        updateLastLogonTime(userName);
        System.out.println("...运行于本服务updateLastLogonTime方法之后...");

        // 代码清单-r5ch12d-406页a：启动独立线程调用事务方法
        System.out.println("...开启一个新的线程运行其他service的方法...");
        Thread myThread = new MyThread(this.scoreService, userName, 20);
        myThread.start();

    }


    public void updateLastLogonTime(String userName) {
        String sql = " UPDATE t_user1 u SET u.last_logon_time = ? WHERE user_name =? ";
        jdbcTemplate.update(sql, System.currentTimeMillis(), userName);
    }

}
